﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TicketingCompany
{
    public partial class Categories : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonCreateCategory_Click(object sender, EventArgs e)
        {
            if (0<TextBoxCategoryName.Text.Length&&TextBoxCategoryName.Text.Length<=50)
            {
                LabelErrorCreate.Visible = false;
                TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
                using (dbContext)
                {
                    dbContext.Categories.AddObject(new Category { Name = TextBoxCategoryName.Text });
                    dbContext.SaveChanges();
                    TextBoxCategoryName.Text = "";
                }
                GridViewCategories.DataBind();
            }
            else
            {
                LabelErrorCreate.Visible = true;
            }
        }

        protected void ButtonShowCreatePanel_Click(object sender, EventArgs e)
        {
            PanelCreateCategory.Visible = !PanelCreateCategory.Visible;
        }

        protected void GridViewCategories_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (GetOperations.IsSafeToDeleteCategoryWithID((int)e.Keys[0]))
            {
                LabelDeleteInfo.Text = "Delete successful.";
            }
            else
            {
                LabelDeleteInfo.Text = "First make sure the category is not associated.";
                e.Cancel = true;
            }
        }

        protected void GridViewCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string name = e.NewValues["Name"].ToString();
            if (1>name.Length||name.Length>50)
            {
                e.Cancel = true;
                LabelDeleteInfo.Text = "Invalid Name Lenght";
            }
            else
            {
                LabelDeleteInfo.Text = "Update successful.";
            }
        }
    }
}